/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.Formatter;

/**
 *
 * @author Rdelacruz
 */
public class NumberUtil {    
    
    private static final DecimalFormatSymbols simbolos;      
    private static final DecimalFormat formatoPorcentaje2Dec;
    private static final DecimalFormat formato2Dec;
    
    static {
        
        //http://chuwiki.chuidiang.org/index.php?title=DecimalFormat    
        //http://stackoverflow.com/questions/5054132/how-to-change-the-decimal-separator-of-decimalformat-from-comma-to-dot-point
        simbolos = new DecimalFormatSymbols();
        simbolos.setDecimalSeparator('.');
        //simbolos.setGroupingSeparator(',');
        
        formatoPorcentaje2Dec = new DecimalFormat("00.00",simbolos);//exactamante 4 digitos
        formato2Dec = new DecimalFormat("#.00", simbolos);//exactamante 2 digitos decimales
    }
    
    public static void main(String arg[]) {
        
        //Formatter f = new Formatter();        
        //System.out.println(f.format("%s", "abc").toString());
        
        System.out.println(intToString(5,5));
        System.out.println(intToString1(5,5));  
        System.out.println(fraccionToPorcentaje2Decimales(0,5,true));
        System.out.println(fraccionToPorcentaje2Decimales(5,0,true));
    }
    
    //http://stackoverflow.com/questions/6034523/format-an-integer-using-java-string-format
    public static String intToString(int num, int digits) {
        Formatter f = new Formatter();
        //return f.format("%03d", num).toString();
        return f.format("%0"+digits+"d", num).toString();
    }
    
    //http://stackoverflow.com/questions/275711/add-leading-zeroes-to-number-in-java
    public static String intToString1(int num, int digits) {
        assert digits > 0 : "Inválido numero de digitos";

        // create variable length array of zeros
        char[] zeros = new char[digits];
        Arrays.fill(zeros, '0');
        
        // format number as String
        DecimalFormat df = new DecimalFormat(String.valueOf(zeros));
        return df.format(num);
        
        //OTRO:
        //("00000000" + "Apple").substring("Apple".length())  --> "000Apple"
        //http://stackoverflow.com/questions/4051887/how-to-format-a-java-string-with-leading-zero
    }
    
    //http://stackoverflow.com/questions/8819842/best-way-to-format-a-double-value-to-2-decimal-places
    //OTRA FORMA: http://stackoverflow.com/questions/5168592/force-a-string-to-2-decimal-places
    public static String doubleToString2Digitos(double decimal) {
        return formato2Dec.format(decimal);
    }
    
    public static String fraccionToPorcentaje2Decimales(int numerador, int denominador, boolean mostrarDivisionEntreCero) {
        return denominador!=0 ? 
                formatoPorcentaje2Dec.format( (double)(numerador*100) / denominador ) : 
               (mostrarDivisionEntreCero?"¡DIV/0!":"-") ;//¡DIV/0! significa division entre cero
    }
    

    public static boolean BooleanToPrimitivo(Boolean valor) {
        return valor!=null && valor;
    }
}
